#include "ClientSock.h"



CClientSock::CClientSock()
{
}


CClientSock::~CClientSock()
{
}

int CClientSock::conn(int svrip, int port)
{
	WSAData wsadata;
	if (0 != WSAStartup(0x0202, &wsadata))
	{
		return false;
	}

	bool bcreateok = false;

	do
	{
		m_sock = WSASocket(AF_INET, SOCK_STREAM, IPPROTO_TCP, NULL, 0, WSA_FLAG_OVERLAPPED);
		if (INVALID_SOCKET == m_sock)
		{
			WriteLog("create listen socket error");
			break;
		}

		DWORD val = 1;
		//ioctlsocket(m_sockListen, FIONBIO, &val);

		SOCKADDR_IN saddr;
		saddr.sin_family = AF_INET;
		saddr.sin_addr.s_addr = svrip;
		saddr.sin_port = htons(port);
		if (SOCKET_ERROR == connect(m_sock, (SOCKADDR*)&saddr, sizeof(saddr)))
		{
			WriteLog("conn fail");
			break;
		}

		const char chOpt = 1;
		int nRet = setsockopt(m_sock, IPPROTO_TCP, TCP_NODELAY, &chOpt, sizeof(char));
		if (SOCKET_ERROR == nRet)
		{
			return FALSE;
		}


		char buf[256];
		memset(buf, 0, sizeof(buf));
		strcpy(buf, "hello svr");
		int len = strlen(buf);
		int ret = send(m_sock, buf, len, 0);
		if (ret == SOCKET_ERROR)
		{
			WriteLog("send 1 fail");
			break;
		}
		WriteLog(buf);

		memset(buf, 0, sizeof(buf));
		ret = recv(m_sock, buf, len, 0);
		if (ret == SOCKET_ERROR)
		{
			WriteLog("recv 1 fail");
			break;
		}
		WriteLog("recv");
		WriteLog(buf);

		system("pause");

		memset(buf, 0, sizeof(buf));
		strcpy(buf, "goodsbye svr");
		len = strlen(buf);
		ret = send(m_sock, buf, len, 0);
		if (ret == SOCKET_ERROR)
		{
			WriteLog("send 2 fail");
			break;
		}
		WriteLog(buf);

		

		memset(buf, 0, sizeof(buf));
		ret = recv(m_sock, buf, len, 0);
		if (ret == SOCKET_ERROR)
		{
			WriteLog("recv 2 fail");
			break;
		}
		WriteLog("recv");
		WriteLog(buf);
	} while (0);
	return 0;
}

int CClientSock::close()
{
	return 0;
}
